package com.hinnotek.warehouse.ui

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.MachineAccountBean
import com.hinnotek.warehouse.bean.MachineListBean
import com.hinnotek.warehouse.databinding.*
import com.hinnotek.warehouse.dialog.Dialogs
import com.hinnotek.warehouse.utils.WAREHOUSE_APPROVED
import com.hinnotek.warehouse.utils.WAREHOUSE_GOODS_TYPE_1
import com.hinnotek.warehouse.utils.WAREHOUSE_TYPE_1
import com.hinnotek.warehouse.utils.WARE_COMPLETE_STATE
import com.hinnotek.warehouse.viewmodel.StockInViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.*
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber


/**
 * 入库 -> 入库单设备列表
 */
@Route(path = ACTIVITY_WAREHOUSE_STOCK_IN_LIST)
@AndroidEntryPoint
class StockInListActivity : BaseActivity<ActivityStockInListBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<MachineListBean, ItemMachineListLayoutBinding>
    private val viewModel by viewModels<StockInViewModel>()

    var machineAccountBean: MachineAccountBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stock_in_list)
        setTitle("入库单设备列表")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.autoRefresh()
        machineAccountBean = intent.getSerializableExtra(MACHINE_CONTENT_LIST) as MachineAccountBean
        bindingView.machineData = machineAccountBean
        viewModel.setParameters(intent.getIntExtra("applyId", 0))
        addCompleteView()
    }

    override fun initVew() {
        adapter = SimpleBindingAdapter(
            R.layout.item_machine_list_layout,
            ArrayList(), BR.machineList
        )
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter

        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }
        //添加adapter
        adapter.addListener { binding, item ->
            binding.toScan.setOnClickListener {
                Skipping.navScan(
                    machineAccountBean?.wareId ?: 0,
                    item.applyDeviceId, WAREHOUSE_TYPE_1, this@StockInListActivity
                )
            }
            binding.root.setOnClickListener {
                if (machineAccountBean?.goodsType ?: "" == WAREHOUSE_GOODS_TYPE_1) {
                    Skipping.start(
                        ACTIVITY_WAREHOUSE_STOCK_IN_DETAIL, "applyDeviceId",
                        item.applyDeviceId, MACHINE_CONTENT_LIST, machineAccountBean,
                        MACHINE_DETAIL_LIST, item, this
                    )
                }
            }
        }
    }


    /**
     * adapter 添加手动完成view
     */
    private fun addCompleteView() {
        if (machineAccountBean == null) {
            return
        }
        if (machineAccountBean?.goodsType == WAREHOUSE_GOODS_TYPE_1) {
            return
        }
        if (machineAccountBean?.completeState == WARE_COMPLETE_STATE) {
            return
        }
        if (adapter.hasFooterLayout()) {
            adapter.removeAllFooterView()
        }
        val bottomBind = DataBindingUtil.inflate<BottomCompleteLayoutBinding>(
            layoutInflater, R.layout.bottom_complete_layout,
            null, false
        )
        bottomBind.complete.setOnClickListener {//完成
            Dialogs(this).showCurrency3("是否完成此仓库单?") { dialog, which ->
                viewModel.toClickComplete()
            }
        }
        adapter.addFooterView(bottomBind.root)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == 200) {
            viewModel.autoRefresh()
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun setData() {
        //列表数据
        viewModel.stockInListPage.observe(this, {
            for (machineListBean in it) {
                machineListBean.approvalState = 2
                machineListBean.goodsType = machineAccountBean?.goodsType ?: ""
            }
            adapter.addAll(it, viewModel.isFirst)
        })
        //完成状态监听
        viewModel.isCompleteData.observe(this, {
            if (viewModel.isSuccess) {
                complete()
            }
        })
        //手动完成此单
        viewModel.clickCompleteData.observe(this, Observer {
            if (viewModel.isSuccess) {
                ToastUtils.showLong("入库成功")
                adapter.removeAllFooterView()
                complete()
            }
        })

    }

    private fun complete() {
        machineAccountBean?.completeState = 2
        bindingView.textView28.text =
            String.format(getString(R.string.complete_StateStr), "已完成")
    }

}